var t, a, e = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../@babel/runtime/regenerator")), n = require("../../../@babel/runtime/helpers/asyncToGenerator"), i = getApp(), o = null, s = !1, c = 0, r = !1, l = null, u = null, h = {}, d = 0, g = null, p = null;

Page({
    data: {
        activeTab: 0,
        chatList: [],
        employeeChatList: [],
        listSetting: {},
        isEmployee: !1,
        chatListPagination: {
            pages: 1,
            finished: !1,
            loading: !1
        },
        employeeChatPagination: {
            pages: 1,
            finished: !1,
            loading: !1
        }
    },
    onLoad: (a = n(e.default.mark(function t(a) {
        return e.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                return t.next = 2, this.getSetting();

              case 2:
                return this.getWebscoketConfig(), this.getEmployeeChatData(), t.next = 6, this.getChatData();

              case 6:
                this.loopGetChatData(), wx.onNetworkStatusChange(function(t) {
                    console.log("onNetworkStatusChange", t.isConnected), console.log("onNetworkStatusChange", t.networkType);
                });

              case 8:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function(t) {
        return a.apply(this, arguments);
    }),
    swichTabTItem: function(t) {
        this.setData({
            activeTab: t.detail.index
        });
    },
    onReady: function() {},
    del_record: function(t) {
        var a, o = this, s = t.detail.id, c = i.getNetAddresss("plugin.yun-chat.frontend.h5.chat.delete");
        i._postNetWork({
            url: c,
            data: {
                queue_id: s
            },
            success: (a = n(e.default.mark(function a(n) {
                var s, c;
                return e.default.wrap(function(a) {
                    for (;;) switch (a.prev = a.next) {
                      case 0:
                        if (s = n.data, c = s.msg, 0 != s.result) {
                            a.next = 3;
                            break;
                        }
                        return a.abrupt("return", i.tips(c));

                      case 3:
                        o.data.chatList && 0 !== o.data.chatList.length && (o.data.chatList = o.data.chatList.filter(function(a) {
                            return a.id !== t.id;
                        }), o.setData({
                            chatList: o.data.chatList
                        })), o.data.employeeChatList && 0 !== o.data.employeeChatList.length && (o.data.employeeChatList = o.data.employeeChatList.filter(function(a) {
                            return a.id !== t.id;
                        }), o.setData({
                            employeeChatList: o.data.employeeChatList
                        }));

                      case 5:
                      case "end":
                        return a.stop();
                    }
                }, a);
            })), function(t) {
                return a.apply(this, arguments);
            })
        });
    },
    getWebscoketConfig: function() {
        var t = this;
        if (this.data.isEmployee) {
            var a = i.getNetAddresss("plugin.yun-chat.frontend.chat.get-ws-setting");
            i._postNetWork({
                url: a,
                success: function(a) {
                    var e = a.data;
                    if (1 !== e.result) return i.tips(e.msg);
                    h = e.data.wss_params, u = "".concat(h.host, "?appId=").concat(h.appId, "&signature=").concat(h.signature, "&nonceStr=").concat(h.nonceStr, "&timestamp=").concat(h.timestamp, "&clientid=").concat(h.clientid, "&agent_id=").concat(h.agent_id), 
                    t.socketInit();
                }
            });
        }
    },
    getSetting: function() {
        var t = this;
        return new Promise(function(a, e) {
            var n = i.getNetAddresss("plugin.yun-chat.frontend.h5.chat.get-list-setting");
            i._postNetWork({
                url: n,
                success: function(e) {
                    var n = e.data;
                    if (1 !== n.result) return i.tips(n.msg);
                    n.data.employee_id > 0 && t.setData({
                        isEmployee: !0
                    }), a();
                }
            });
        });
    },
    socketInit: function() {
        var t = this;
        console.log("执行socket链接", u), null === l && ((l = wx.connectSocket({
            url: u
        })).onMessage(function(a) {
            var e = JSON.parse(a.data);
            if (console.log(e), e.uid) {
                var n = 0, i = t.data.employeeChatList, o = i.find(function(t, a) {
                    if (t.uid == e.uid) return n = a, t;
                });
                if (console.log(n, o), o) {
                    switch (e.content_type) {
                      case 0:
                        o.last_chat_content = e.data;
                        break;

                      case 1:
                        o.last_chat_content = "[图片]";
                        break;

                      case 2:
                        o.last_chat_content = "[商品]";
                        break;

                      case 3:
                        o.last_chat_content = "[订单]";
                    }
                    o.un_read_num++, o.updated_at = e.time, i.splice(n, 1), i.unshift(o), t.setData({
                        employeeChatList: i
                    });
                } else i.unshift(e), t.setData({
                    employeeChatList: i
                });
            }
        }), l.onError(function(a) {
            l.close(), t.socketReconnet(), l = null;
        }), l.onOpen(function() {
            t.checkSocketConnectState();
        }), l.onClose(function() {
            clearInterval(p), p = null;
        }));
    },
    checkSocketConnectState: function() {
        var t = this;
        null === p && (p = setInterval(function() {
            l ? 1 == l.readyState ? (console.log(JSON.stringify({
                type: "ping"
            })), l.send({
                data: JSON.stringify({
                    type: "ping"
                })
            }), d = 0) : (clearInterval(p), p = null, l.close(), t.socketReconnet()) : (clearInterval(p), 
            p = null);
        }, 5e3));
    },
    socketReconnet: function() {
        var t = this;
        null === g && (g = setInterval(function() {
            d++, t.socketInit(), d >= 10 && (clearInterval(g), g = null);
        }, 5e3));
    },
    getChatData: function() {
        var t = this;
        return new Promise(function(a, e) {
            if (s || !t.data.chatListPagination.finished && !t.data.chatListPagination.loading) {
                var n = t.data.chatListPagination.pages;
                s ? n = 1 : t.data.chatListPagination.loading = !0;
                var o = i.getNetAddresss("plugin.yun-chat.frontend.h5.chat.get-list");
                i._postNetWork({
                    url: o,
                    data: {
                        page: n
                    },
                    success: function(e) {
                        var o, c = e.data, l = c.data, u = l.data, h = l.per_page, d = l.current_page, g = l.last_page, p = c.msg, f = c.result;
                        if (t.data.chatListPagination.loading = !1, 1 !== f) return i.tips(p);
                        o = 1 == n ? u : t.data.chatList.concat(u), t.setData({
                            chatList: o
                        }), s ? (s = !1, r = !1, t.data.chatListPagination.pages = 1) : ((u.length < h || d == g) && (t.data.chatListPagination.finished = !0), 
                        t.data.chatListPagination.pages++), a();
                    }
                });
            }
        });
    },
    loopGetChatData: function() {
        var t = this;
        clearInterval(o), c = 0, null === (o = null) ? o = setInterval(function() {
            console.log(c), !1 === r && (s = !0, 1 == t.data.isEmployee && 0 == t.data.activeTab ? t.getEmployeeChatData() : t.getChatData(), 
            ++c >= 100 && (clearInterval(o), o = null));
        }, 5e3) : c = 0;
    },
    getEmployeeChatData: function() {
        var t = this;
        if (s || !1 !== this.data.isEmployee && !this.data.employeeChatPagination.finished && !this.data.employeeChatPagination.loading) {
            var a = this.data.employeeChatPagination.pages;
            s ? a = 1 : this.data.employeeChatPagination.loading = !0;
            var e = i.getNetAddresss("plugin.yun-chat.frontend.chat.get-list");
            i._postNetWork({
                url: e,
                data: {
                    page: a
                },
                success: function(e) {
                    var n, o = e.data, c = o.data, l = c.data, u = c.per_page, h = c.current_page, d = c.last_page;
                    if (t.data.employeeChatPagination.loading = !1, 1 !== o.result) return i.tips(o.msg);
                    (l.length < u || h == d) && (t.data.employeeChatPagination.finished = !0), n = 1 == a ? l : t.data.employeeChatList.concat(l), 
                    t.setData({
                        employeeChatList: n
                    }), s ? (s = !1, r = !1, t.data.employeeChatPagination.pages = 1) : ((l.length < u || h == d) && (t.data.employeeChatPagination.finished = !0), 
                    t.data.employeeChatPagination.pages++);
                }
            });
        }
    },
    toEmployeeChat: function(t) {
        var a = t.currentTarget.dataset.item;
        wx.navigateTo({
            url: "/packageH/chitchat/chatWindow/chatWindow?chatType=2&user_id=" + a.uid
        });
    },
    enterChatPage: function(t) {
        var a = t.currentTarget.dataset.item;
        wx.navigateTo({
            url: "/packageH/chitchat/chatWindow/chatWindow?chatType=1&groupId=" + a.group_id + "&employeeId=" + a.employee_id
        });
    },
    onShow: (t = n(e.default.mark(function t() {
        var a, n;
        return e.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                return a = {
                    pages: 1,
                    finished: !1,
                    loading: !1
                }, n = {
                    pages: 1,
                    finished: !1,
                    loading: !1
                }, this.setData({
                    chatListPagination: a,
                    employeeChatPagination: n
                }), this.getEmployeeChatData(), t.next = 6, this.getChatData();

              case 6:
                this.loopGetChatData();

              case 7:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function() {
        return t.apply(this, arguments);
    }),
    onHide: function() {
        clearInterval(o), o = null;
    },
    onUnload: function() {
        l && l.close(), clearInterval(o), o = null;
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        1 == this.data.isEmployee && 0 == this.data.activeTab ? this.getEmployeeChatData() : this.getChatData();
    },
    onShareAppMessage: function() {}
});